//
//  NextViewController.m
//  07-通信传值
//
//  Created by dllo on 16/7/22.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "NextViewController.h"


@interface NextViewController ()
@property (nonatomic, strong) UITextField *text;
@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 50);
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor brownColor];
    
    self.text = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 100, 50)];
    _text.placeholder = @"不好";
    _text.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_text];
    // 正面属性传值
    _text.text = self.titleString;

}

- (void)click {
    // navigation中的viewcontrollers数组,数组对象有引用计数 + 1
    // 只要不出栈,就不会释放,就能在navigation的viewcontrollers数组里获取到
    // 只有pop此视图,该视图被释放,不能获取到该视图控制器
    // 反面属性传值
    NSInteger count = self.navigationController.viewControllers.count;
    // 获取当前导航控制器的入栈视图(根视图,next视图)
    ViewController *vc = self.navigationController.viewControllers[count - 2];
    vc.textString = _text.text;
   
    // 触发之前判断
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendValue:)]) {
        [self.delegate sendValue:_text.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
